﻿using UnityEngine;
using System.Collections;

public class ScreenManager : MonoBehaviour 
{
	Transform[] Screens;
	[SerializeField] Transform MainMenuScreen;
	[SerializeField] Transform StoryBoardScreen;
	[SerializeField] Transform InGameScreen;
	[SerializeField] Transform GameOverScreen;
	[SerializeField] Transform AchievementsScreen;
	[SerializeField] Transform ShopScreen;

	void OnEnable () 
	{
		StateManager.OnMainMenuScreen += HandleOnMainMenuScreen;
		StateManager.OnStoryboardScreen += HandleOnStoryboardScreen;
		StateManager.OnInGameScreen += HandleOnInGameScreen;
		StateManager.OnGameOverScreen += HandleOnGameOverScreen;
		StateManager.OnAchievementsScreen += HandleOnAchievementsScreen;
		StateManager.OnShopScreen += HandleOnShopScreen;

		//Add Available Screens here
		Screens = new Transform[] { MainMenuScreen, 
			StoryBoardScreen,
			InGameScreen,
			GameOverScreen,
			AchievementsScreen,
			ShopScreen };
	}

	void OnDisable ()
	{
		StateManager.OnMainMenuScreen -= HandleOnMainMenuScreen;
		StateManager.OnStoryboardScreen -= HandleOnStoryboardScreen;
		StateManager.OnInGameScreen -= HandleOnInGameScreen;
		StateManager.OnGameOverScreen -= HandleOnGameOverScreen;
		StateManager.OnAchievementsScreen -= HandleOnAchievementsScreen;
		StateManager.OnShopScreen -= HandleOnShopScreen;
	}

	void HandleOnMainMenuScreen ()
	{
		if ( !MainMenuScreen.gameObject.activeSelf )
		{
			EnableScreens (MainMenuScreen);
		}
	}
	
	void HandleOnStoryboardScreen ()
	{
		if ( !StoryBoardScreen.gameObject.activeSelf )
		{
			EnableScreens (StoryBoardScreen);
		}
	}
	
	void HandleOnInGameScreen ()
	{
		if ( !InGameScreen.gameObject.activeSelf )
		{
			EnableScreens (InGameScreen);
		}
	}
	
	void HandleOnGameOverScreen ()
	{
		if ( !GameOverScreen.gameObject.activeSelf )
		{
			EnableScreens (GameOverScreen);
		}
	}
	
	void HandleOnAchievementsScreen ()
	{
		if ( !AchievementsScreen.gameObject.activeSelf )
		{
			EnableScreens (AchievementsScreen);
		}
	}
	
	void HandleOnShopScreen ()
	{
		if ( !ShopScreen.gameObject.activeSelf )
		{
			EnableScreens (ShopScreen);
		}
	}

	// Enable/Disable Screens
	void EnableScreens (Transform screen)
	{
		foreach ( Transform s in Screens )
		{
			if ( s == screen )
				s.gameObject.SetActive ( true );
			else
				s.gameObject.SetActive ( false );
		}
	}
}
